package com.magicsoft.app.wcf;

import java.lang.reflect.Type;

import org.apache.http.Header;
import org.json.JSONObject;

import android.content.Context;
import android.util.Log;

import com.boyuan.huijiaoyu.R;
import com.google.gson.reflect.TypeToken;
import com.loopj.android.http.JsonHttpResponseHandler;
import com.magicsoft.app.entity.WeatherResp;
import com.magicsoft.app.helper.DateUtils;
import com.magicsoft.app.helper.SharePreferenceHelper;
import com.magicsoft.app.wcf.listener.GetOneRecordListener;

public class AdweatherService extends BaseService {

	public AdweatherService(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
	}

	/**
	 * 
	 * @param lat维度
	 * @param lng
	 *            经度
	 * @param listener
	 */
	public void getWeather(String lat, String lng,
			final GetOneRecordListener<WeatherResp> listener) {

		// params.put("lat", "40.128181");
		// params.put("lng", "116.009533");
		// String getParamString = "";

		// try {
		// getParamString = TokenHelper.FetchKaKaToolToken(context);
		// } catch (Exception e) {
		//
		// }

		// String requestUrl = Constant.ADWEATHER_URL + "?lng=" + lng + "&lat="
		// + lat;
		//
		Log.i("AD_WEATHER", "lat = " + lat + ", lng = " + lng);
		Log.e("sssss", "http://api.map.baidu.com/telematics/v3/weather?location="
				+ lng + "," + lat
				+ "&output=json&ak=KsSPzG8Y72VhGUXhc0iGgCDm");
		AsyncHttpServiceHelper.get(
				"http://api.map.baidu.com/telematics/v3/weather?location="
						+ lng + "," + lat
						+ "&output=json&ak=KsSPzG8Y72VhGUXhc0iGgCDm",
				new JsonHttpResponseHandler() {

					@Override
					public void onSuccess(int statusCode, Header[] headers,
							JSONObject response) {
						// TODO Auto-generated method stub
						super.onSuccess(statusCode, headers, response);

						Log.i("AD_WEATHER", response.toString());

						try {

							String message = response.get("error").toString();
							String status = response.get("status").toString();

							if (status.equalsIgnoreCase("success")) {

								JSONObject weatherJson = (JSONObject) response
										.getJSONArray("results").get(0);

								Type wearherType = new TypeToken<WeatherResp>() {
								}.getType();
								WeatherResp wearherresp = gson.fromJson(
										weatherJson.toString(), wearherType);

								if (wearherresp != null) {

									SharePreferenceHelper
											.saveWeatherResp(
													context,
													wearherresp,
													SharePreferenceHelper.WeiTown_weather_entity);
									SharePreferenceHelper
											.setLongValue(
													context,
													SharePreferenceHelper.WeiTown_weather_time,
													DateUtils.getCurrentDate());
									if (listener != null)
										listener.onFinish(wearherresp);
								} else {
									if (listener != null)

										listener.onFailed(message);

								}
							} else {
								if (listener != null)

									listener.onFailed(message);

							}

						} catch (Exception e) {

							if (listener != null)

								listener.onFailed(context
										.getResources()
										.getString(R.string.json_analysis_wrong));// JSON
																					// 解析错误
						}
					}

					@Override
					public void onFailure(int statusCode, Header[] headers,
							String responseString, Throwable throwable) {
						// TODO Auto-generated method stub
						super.onFailure(statusCode, headers, responseString,
								throwable);
						if (listener != null)
								listener.onFailed(context
										.getResources()
										.getString(R.string.network_connections_wrong));

					}

					@Override
					public void onFailure(int statusCode, Header[] headers,
							Throwable throwable, JSONObject errorResponse) {
						// TODO Auto-generated method stub
						super.onFailure(statusCode, headers, throwable,
								errorResponse);
						if (listener != null)
								listener.onFailed(context
										.getResources()
										.getString(R.string.network_connections_wrong));
					}
				});

	}

}
